﻿using BasicData.API.Read.Applications.Commands;
using BasicData.API.Read.Dtos;
using BasicData.Domain;
using BasicData.ErrorCode;
using BasicData.Infrastrueturc.Command;
using BasicData.Infrastrueturc.Interfaces;
using MediatR;

namespace BasicData.API.Read.Applications.CommandHandlers
{
    public class MyPendingQueryCommandHandler : IRequestHandler<MyPendingQueryCommand, APIResult<APIPaging<InstanceDTO>>>
    {
        private readonly IBaseReposiotry<WF_Instance> InstanceReposiotry;
        private readonly IBaseReposiotry<Room> roomReposiotry;
        public MyPendingQueryCommandHandler(IBaseReposiotry<WF_Instance> InstanceReposiotry, IBaseReposiotry<Room> roomReposiotry)
        {
            this.InstanceReposiotry = InstanceReposiotry;
            this.roomReposiotry = roomReposiotry;
        }

        public Task<APIResult<APIPaging<InstanceDTO>>> Handle(MyPendingQueryCommand request, CancellationToken cancellationToken)
        {
           APIResult<APIPaging<InstanceDTO>> result=new APIResult<APIPaging<InstanceDTO>>();
            result.Code=ResultCode.查询成功;
            result.Message= ResultCode.查询成功.ToString();
            var list = from a in InstanceReposiotry.GetAll()
                       join b in roomReposiotry.GetAll() on a.InforId equals b.RoomId
                       where a.NextOperatorId == Convert.ToInt32(request.UserId) && a.FlowStatus < (int)FlowStatus.已完成
                       select new InstanceDTO
                       {
                           InstanceId=a.InstanceId,
                           InforId=a.InforId,
                           FlowStatus=a.FlowStatus,
                           FlowStatusName=a.FlowStatusName,
                           StarterId=a.StarterId,
                           StarterName=a.StarterName,
                           CurrentNodeNo=a.CurrentNodeNo,
                           CurrentNodeName=a.CurrentNodeName,
                           CurrentOperatorId=a.CurrentOperatorId,
                           CurrentOperatorName=a.CurrentOperatorName,   
                           NextNodeNo=a.NextNodeNo,
                           NextNodeName=a.NextNodeName,
                           NextOperatorId=a.NextOperatorId,
                           NextOperatorName=a.NextOperatorName,
                           CreateTime=a.CreateTime,
                           UpdateTime=a.UpdateTime,
                           RoomName=b.RoomName,
                           RoomArea=b.RoomArea,
                           RoomComment=b.RoomComment,

                       };

            int TotelCount=list.Count();
            int TotelPcount = (int)Math.Ceiling(TotelCount * 1.0 / request.PageSize);
            list=list.OrderBy(x=>x.InstanceId).Skip((request.PageIndex-1)*request.PageSize).Take(request.PageSize);
            APIPaging<InstanceDTO> comm=new APIPaging<InstanceDTO>();
            comm.TotelCount=TotelCount;
            comm.TotelPcount=TotelPcount;
            comm.PageData = list.ToList();
            result.Data= comm;  

            return Task.FromResult(result);
        }
    }
}
